<?php

namespace app\admin\controller;

class Home extends Base
{
    public function index()
    {
        return view();
    }

    public function cateAdd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Cate')->add($data);
            if ($result == 1) {
                $this->success('导航添加成功！', 'admin/home/catelist');
            }else {
                $this->error($result);
            }
        }
        return view('cateadd');
    }

    public function cateList()
    {
        $cateInfo = model('Cate')->paginate(5);
        $viewData = [
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view('catelist');
    }

    public function cateEdit()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Cate')->edit($data);
            if ($result == 1) {
                $this->success('导航修改成功！', 'admin/home/catelist');
            }else {
                $this->error($result);
            }
        }
        $id = input('id');
        $cateInfo = model('Cate')->where('id', $id)->find();
        $viewData = [
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view('cateedit');
    }

    public function cateDel()
    {
        $id = input('id');
        $result = model("cate")->where('id', $id)->delete();
        if ($result) {
            $this->success('导航删除成功！', 'admin/home/catelist');
        }else {
            $this->error('导航删除失败！');
        }
    }

    public function articleAdd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Article')->add($data);
            if ($result == 1) {
                $this->success('文章添加成功！', 'admin/home/articlelist');
            }else {
                $this->error($result);
            }
        }
        $catesInfo = model('Cate')->select();
        $viewData = [
            'catesInfo' => $catesInfo
        ];
        $this->assign($viewData);
        return view('articleadd');
    }

    public function articleList()
    {
        $articlesInfo = model('Article')->with('cate')->paginate(5);
        $viewData = [
            'articlesInfo' => $articlesInfo
        ];
        $this->assign($viewData);
        return view('articlelist');
    }

    public function articleEdit()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Article')->edit($data);
            if ($result == 1) {
                $this->success('文章修改成功！', 'admin/home/articlelist');
            }else {
                $this->error($result);
            }
        }
        $catesInfo = model('Cate')->select();
        $id = input('id');
        $articleInfo = model('Article')->where('id', $id)->find();
        $viewData = [
            'catesInfo' => $catesInfo,
            'articleInfo' => $articleInfo
        ];
        $this->assign($viewData);
        return view('articleedit');
    }

    public function articleDel()
    {
        $result = model('Article')->where(input('post.'))->delete();
        if ($result) {
            $this->success('文章删除成功！', 'admin/home/articlelist');
        }else {
            $this->error('文章删除失败！');
        }
    }

    public function userAdd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('User')->add($data);
            if ($result == 1) {
                $this->success('会员添加成功！', 'admin/home/userlist');
            }else {
                $this->error($result);
            }
        }
        return view('useradd');
    }

    public function userList()
    {
        $usersInfo = model('User')->paginate(5);
        $viewData = [
            'usersInfo' => $usersInfo
        ];
        $this->assign($viewData);
        return view('userlist');
    }

    public function userEdit()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('User')->edit($data);
            if ($result == 1) {
                $this->success('会员修改成功！', 'admin/home/userlist');
            }else {
                $this->error($result);
            }
        }
        $userInfo = model('User')->where('id', input('id'))->find();
        $viewData = [
            'userInfo' => $userInfo
        ];
        $this->assign($viewData);
        return view('useredit');
    }

    public function userDel()
    {
        $result = model('User')->where(input('post.'))->delete();
        if ($result) {
            $this->success('删除会员成功！', 'admin/home/userlist');
        }else {
            $this->error('删除会员失败！');
        }
    }

    public function system()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('System')->edit($data);
            if ($result == 1) {
                $this->success('系统设置成功！', 'admin/home/index');
            }else {
                $this->error($result);
            }
        }
        $systemInfo = model('System')->find();
        $viewData = [
            'systemInfo' => $systemInfo
        ];
        $this->assign($viewData);
        return view();
    }
}
